using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace IUPUI.StellarResults.Client.UIElements
{
	/// <summary>
	/// A legend of <see cref="AstronomicalObject"/>s.
	/// </summary>
	public partial class AstronomicalObjectsLegendControl : FlowLayoutPanel
	{
		#region Private Properties
		private Simulation m_Simulation;
		#endregion Private Properties

		#region Public Properties
		/// <summary>
		/// Gets or sets the simulation.
		/// </summary>
		/// <value>The simulation.</value>
		[Browsable(false)]
		public Simulation Simulation
		{
			get
			{
				return m_Simulation;
			}
			set
			{
				if (m_Simulation != value)
				{
					m_Simulation = value;
					OnSimulationChanged();
				}
			}
		}
		#endregion Public Properties

		#region Constructors
		/// <summary>
		/// Initializes a new instance of the <see cref="AstronomicalObjectsLegendControl"/> class.
		/// </summary>
		public AstronomicalObjectsLegendControl()
		{
			InitializeComponent();
		}

		/// <summary>
		/// Initializes a new instance of the <see cref="AstronomicalObjectsLegendControl"/> class.
		/// </summary>
		/// <param name="container">The container.</param>
		public AstronomicalObjectsLegendControl(
			IContainer container) : this()
		{
			container.Add(this);
		}
		#endregion Constructors

		/// <summary>
		/// Handles the Modified event of the m_AstronomicalObjects_Modified control.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
		private void m_AstronomicalObjects_Modified(object sender, EventArgs e)
		{
			OnAstronomicalObjectsChanged();
		}

		/// <summary>
		/// Called when [simulation changed].
		/// </summary>
		private void OnSimulationChanged()
		{
			if (m_Simulation != null)
			{
				m_Simulation.AstronomicalObjects.Modified += 
					new EventHandler(m_AstronomicalObjects_Modified);
				OnAstronomicalObjectsChanged();
			}
		}

		/// <summary>
		/// Called when [astronomical objects changed].
		/// </summary>
		private void OnAstronomicalObjectsChanged()
		{
			this.Controls.Clear();

			if (m_Simulation != null)
			{
				m_Simulation.AstronomicalObjects.Modified += new EventHandler(m_AstronomicalObjects_Modified);
				// Add each astronomical object
				foreach (AstronomicalObject o in m_Simulation.AstronomicalObjects)
				{
					this.Controls.Add(new AstronomicalObjectLegendItem(o, m_Simulation));
				}
			}
		}
	}
}
